using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Barebones.Components;
using Microsoft.Xna.Framework.Input;

namespace Meat.Input
{
    public class KeyboardReader : EngineComponent, Barebones.Framework.IUpdateable
    {
        KeyboardState m_Prev;

        public override IEnumerable<Barebones.Dependencies.IDependency> GetDependencies()
        {
            yield break;
        }

        public void Update(float dt)
        {
            KeyboardState state = Keyboard.GetState();

            Keys[] prev = m_Prev.GetPressedKeys();
            Keys[] current = state.GetPressedKeys();

            foreach( Keys key in prev.Except(current) ) //released keys
            {
                Owner.Forum.Fire(new KeyReleased() { Key = key });
            }

            foreach (Keys key in current.Except(prev)) //pressed keys
            {
                Owner.Forum.Fire(new KeyPressed() { Key = key });
            }

            m_Prev = state;
        }

        public bool IsKeyDown(Keys key)
        {
            return m_Prev.IsKeyDown(key);
        }
        public bool IsKeyUp(Keys key)
        {
            return m_Prev.IsKeyUp(key);
        }
    }
}
